What is node-gzip?
The node-gzip package provides simple and efficient methods for compressing and decompressing data using the Gzip algorithm in Node.js applications.
What are node-gzip's main functionalities?
Compress Data
This feature allows you to compress a string or buffer using the Gzip algorithm. The code sample demonstrates how to compress a simple string 'Hello, World!' and log the compressed data.
const { gzip } = require('node-gzip');
const data = 'Hello, World!';
async function compressData() {
const compressed = await gzip(data);
console.log(compressed);
}
compressData();
Decompress Data
This feature allows you to decompress Gzip-compressed data back to its original form. The code sample shows how to decompress previously compressed data and log the decompressed string.
const { ungzip } = require('node-gzip');
const compressedData = /* some compressed data */;
async function decompressData() {
const decompressed = await ungzip(compressedData);
console.log(decompressed.toString());
}
decompressData();
Other packages similar to node-gzip
zlib
The zlib package is a core Node.js module that provides compression and decompression functionalities using the Gzip and Deflate algorithms. It offers more control and options compared to node-gzip but requires more boilerplate code.
pako
Pako is a fast and efficient compression library that provides Gzip and Deflate algorithms for both Node.js and browser environments. It offers similar functionalities to node-gzip but with additional support for streaming and more advanced use cases.
gzip-js
gzip-js is a pure JavaScript implementation of the Gzip algorithm. It is suitable for environments where native modules are not available or desired. However, it is generally slower and less efficient compared to node-gzip and other native solutions.
node-gzip
Gzip and ungzip in Node.js
Tiny and easy to use wrapper around zlib.gzip and zlib.gunzip to support promises.
const compressed = await gzip('Hello World');
Install
npm install node-gzip --save
Examples
With Promises
const {gzip, ungzip} = require('node-gzip');
gzip('Hello World')
.then((compressed) => {
return ungzip(compressed);
})
.then((decompressed) => {
console.log(decompressed.toString());
});
With async / await
const {gzip, ungzip} = require('node-gzip');
const compressed = await gzip('Hello World');
const decompressed = await ungzip(compressed);
console.log(decompressed.toString());
Options
Pass options just like with Zlib. See all options.
await gzip('Hello World', {...});
Description
gzip(input[,options])
- input:
Buffer | TypedArray | DataView | ArrayBuffer | string
- returns:
Buffer
ungzip(input[,options])
- input:
Buffer | TypedArray | DataView | ArrayBuffer | string
- returns:
Buffer
Use toString()
after ungzip
to convert the Buffer into a string.
Supports Node.js version 0.12 and higher.
License
node-gzip is MIT licensed.